﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using CompanyBussiness;

namespace Website.admin.Control
{
    public partial class cPermissionAccount : System.Web.UI.UserControl
    {
        AccountManagement accountMana = new AccountManagement();
        MD5 md5 = new MD5();
        CompanyBussiness.Account account = new Account();
        CompanyBussiness.AccountLog accountLog = new AccountLog();
        CompanyBussiness.InternalUser internerUser = new InternalUser();
        CompanyBussiness.Modul modul = new Modul();
        string ipaddress;
        string pass;
        int accountID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request["acc"] == null)
                    return;


                accountID = Convert.ToInt32(Request["acc"].ToString());
                try
                {
                    GridViewList.DataSource = accountMana.GetListModul();
                    GridViewList.DataBind();

                }
                catch (Exception ex)
                {

                }
            }
            
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            accountID = Convert.ToInt32(Request["acc"].ToString());

            for (int i = 0; i < GridViewList.Rows.Count; i++)
            {
                modul = accountMana.GetModulByID(int.Parse(GridViewList.DataKeys[i].Value.ToString()));
                bool status = ((CheckBox)GridViewList.Rows[i].FindControl("checkStatus")).Checked;

                VwPermissionUser vw = new VwPermissionUser();
                vw = accountMana.GetVwPerUserByAccountID(accountID);

                internerUser = new InternalUser();
                internerUser.InternalID = vw.InternalID;

                if (status == false)
                {
                    if (accountMana.GetPermissionByAllID(internerUser, modul) > 0)
                    {
                        accountMana.DeletePermissionByAllID(internerUser, modul);
                    }
                }
                else
                {
                    if (accountMana.GetPermissionByAllID(internerUser, modul) == 0)
                    {
                        accountMana.SetPermission(internerUser, modul);
                    }
                }
                
                
            }

            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "AccountMana";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=AccountMana&Mod=" + "MACCOUNT");
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            Response.Cookies[CONST.ACTION][CONST.COOKIES_ACTION_NAME] = "AccountMana";
            Response.Redirect("Administrator.aspx?" + CONST.ACTION + "=AccountMana&Mod=" + "MACCOUNT");
        }

        protected void GridViewList_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Modul obj = (Modul)e.Row.DataItem;

                CheckBox checkStatus = (CheckBox)e.Row.FindControl("checkStatus");
                Label lbModulName = (Label)e.Row.FindControl("lbModulName");

                List<VwPermissionUser> listPU = accountMana.GetPermissionUserByAccountID(accountID);
                foreach (var t in listPU)
                {
                    if (t.ModulName == lbModulName.Text)
                    {
                        checkStatus.Checked = true;
                    }
                   
                        
                }
                
            }
            else
                return;
        }
    }
}